#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=8e3+10,INF =INT_MAX;
ll g[N][N];
ll n;
void floyd(){
  for (ll k=1;k<=n;k++){
    for (ll i=1;i<=n;i++){
      for (ll j=1;j<=n;j++){
        if (g[i][k]!=INF && g[k][j]!=INF){
          if (g[i][k]+g[k][j]<g[i][j]){
            g[i][j]=g[i][k]+g[k][j];
          }
        }
      }
    }
  }
}
int main(){
  ll k;
  cin>>n>>k;
  memset(g,INF,sizeof g);
  for (int i=1;i<n;i++){
    ll a,b,c;
    cin>>a>>b>>c;
    g[a][b]=c;
    g[b][a]=c;
  }
  floyd();
  vector<ll> arr(k);
  for (ll i=0;i<k;i++)cin>>arr[i];
  vector<ll> p=arr;
  for (ll i=0;i<k;i++){
      arr.erase(arr.begin()+i);
      ll ans=0;
      for (ll j=0;j<k-1;j++){
          ll u=arr[j],v=arr[j+1];
          ans+=g[u][v];
      }
      cout<<ans<<" ";
      arr=p;
  }
  return 0;
}
